
var condition={
    keyword:"",
    city:"",
    method:"跟团游",
    days:"",
    startCity:"",
    startDate:"",
    feature:"",
    price:""
}
var sortStatus =0;//0:默认排序；1：产品销量排序；2：产品星级排序；
                 // 3：产品价格升序；4：产品价格降序
var conditionArr = new Array();

$(".local-city").text($(".city-name").text());

function selected(event){
    var clickEle=event.target;
    if (clickEle.parentElement.className=="condition-second"){
        //=================点谁谁变色================
        var lis=clickEle.parentElement.children;
        for (var i=0;i<lis.length;i++){
            lis[i].style.color="#1d1d1d";
        }
        clickEle.style.color="#00b3ee";
        //获得数据
        var lis=document.querySelector(".condition-first").children;
        for (var i=0;i<lis.length;i++){
            if (lis[i].firstElementChild.innerText==clickEle.parentElement.previousElementSibling.innerText) {
                if (clickEle.innerText !="不限")
                    conditionArr[i]=clickEle.innerText;
                else
                    conditionArr[i]="";
            }
        }

        //===============将筛选条件拼接=========================

        var city=$(".city-name").text();
        var selected=""
        condition.keyword=typeof(keyword)=="undefined"?"":keyword;
        condition.city = typeof(city)=="undefined"?"":city;
        condition.method=typeof(conditionArr[0])=="undefined"?"":conditionArr[0];
        condition.days=typeof(conditionArr[1])=="undefined"?"":conditionArr[1];
        condition.startCity=typeof(conditionArr[2])=="undefined"?"":conditionArr[2];
        condition.startDate=typeof(conditionArr[3])=="undefined"?"":conditionArr[3];
        condition.feature=typeof(conditionArr[4])=="undefined"?"":conditionArr[4];
        condition.price=typeof(conditionArr[5])=="undefined"?"":conditionArr[5];

        if (condition.keyword!="")
            selected=selected+condition.keyword+";";

        if (condition.method!="")
            selected=selected+condition.method+";";
        if (condition.days!="")
            selected=selected+condition.days+";";
        if (condition.startCity!="")
            selected=selected+condition.startCity+";";
        if (condition.startDate!="")
            selected=selected+condition.startDate+";";
        if (condition.feature!="")
            selected=selected+condition.feature+";";
        if (condition.price!="")
            selected=selected+condition.price+";";
        document.querySelector(".condition-selected").value=selected;
        if (condition.method=="")
            condition.method="跟团游"
        //========================多条件查询=====================
        getall(1);
    }
}



//============获取筛选数据===========
function getData(){
    var keyword=$(".search-key-input").val();
    var data = {
        keyword:keyword,
        method:condition.method,
        days:"",
        startCity:condition.startCity,
        feature:condition.feature,
        minPrice:"",
        maxPrice:""
    }
    //=============天数============
    switch (condition.days) {
        case "":data.days ="";break;
        case "3天": data.days = 3;break;
        case "4天": data.days = 4;break;
        case "5天": data.days = 5;break;
        case "6天": data.days = 6;break;
        case "7天": data.days = 7;break;
        case "8天": data.days = 8;break;
        case "10天以上": data.days = 10;break;
    }
    // //=============价格============
    switch (condition.price) {
        case "":data.minPrice =0,data.maxPrice=100000;break;
        case "1001元-2000元/人":data.minPrice =1001,data.maxPrice=2000;break;
        case "2001元-3000元/人":data.minPrice =2001,data.maxPrice=3000;break;
        case "3001元-4000元/人":data.minPrice =3001,data.maxPrice=4000;break;
        case "4001元-5000元/人":data.minPrice =4001,data.maxPrice=5000;break;
        case "5000元以上/人":data.minPrice =5001,data.maxPrice=100000;break;
    }
    return data;
}

// ===========================多条件ajax 分页查询==========================
function getall(pageNum ){
    //========================生成筛选数据=====================
    var data = getData();
    var url ="/product/all_search?pageNum="+pageNum+"&sortStatus="+sortStatus;

    $.ajax({
        type:"POST",
        url:url,
        data:data,
        datatype:"json",
        success:function(result){
            //渲染页码
            var pageNum = result.data.pageNum;//当前页码
            var total = result.data.total;//总记录数
            var pages = result.data.pages;//总页码

            $(".pageNum").text(pageNum);
            $(".pages").text(pages);
            $(".total").text(total);





            $(".result-list").html("");
            console.log("====sortStatus="+sortStatus)
            var productList =result.data.list;
            console.log("========多条件查询请求发送成功！结果为：result："+productList.length)
            for (var i =0;i<productList.length;i++){
                ele=`<li class="list-group-item result-item">
                <div class="product-list">
                <a href="/product/getOne/${productList[i].id}">
                    <div class="mark-type">${data.method}</div>
                   
                    <img src="../${productList[i].previewImg}" class="product-img" alt="">
                    <input type="hidden" class="productId" value="${productList[i].id}">
                    <a href="/product/getOne/${productList[i].id}" class="product-des">
                        ${productList[i].productName}</a>
                    <div class="product-pri">￥<span class="product-price">${productList[i].personPrice}</span>起/份</div>
                    <ul class="product-info">
                        <li>${productList[i].productDescription}</li>
                        <li>当地游</li>
                        <li>周到服务...(包含早餐,免费...) | 舒心旅游 | 豪华住宿</li>
                    </ul>
                    <div class="favorite favorite-reduce" style="display: none"><svg t="1585624213763" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4623" width="16" height="16"><path d="M742.4 64c-89.088 0-174.592 42.368-230.4 109.056C456.192 106.368 370.688 64 281.6 64 123.648 64 0 190.336 0 351.744c0 197.504 174.08 358.912 437.76 603.52L512 1024l74.24-68.8C849.92 710.656 1024 549.248 1024 351.68 1024 190.464 900.352 64 742.4 64z" fill="#FF001F" p-id="4624"></path></svg>取消收藏</div>
                    <div class="favorite favorite-add" ><svg t="1585624256936" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5647" width="16" height="16"><path d="M512 966.4l-19.2-25.6-422.4-416C25.6 473.6 0 403.2 0 339.2 0 185.6 121.6 64 268.8 64c102.4 0 192 57.6 243.2 147.2C563.2 121.6 652.8 64 755.2 64 902.4 64 1024 185.6 1024 339.2c0 70.4-25.6 134.4-70.4 185.6L512 966.4zM268.8 128C160 128 64 224 64 339.2c0 51.2 19.2 102.4 57.6 140.8L512 876.8 908.8 480c32-38.4 51.2-89.6 51.2-140.8C960 224 864 128 755.2 128c-76.8 0-147.2 44.8-185.6 108.8L512 345.6l-57.6-108.8C416 172.8 345.6 128 268.8 128z" fill="#FF002C" p-id="5648"></path></svg>添加收藏</div>
                    <div class="product-review"><span class="score">${productList[i].productStar}</span><svg t="1586248824380" class="icon" viewBox="0 0 1102 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2140" width="15" height="15"><path d="M533.070769 856.418462l-258.284307 135.758769a39.384615 39.384615 0 0 1-57.147077-41.511385l49.309538-287.586461a39.384615 39.384615 0 0 0-11.303385-34.855385l-208.974769-203.697231a39.384615 39.384615 0 0 1 21.819077-67.190154l288.768-41.944615a39.384615 39.384615 0 0 0 29.656616-21.543385l129.181538-261.671384a39.384615 39.384615 0 0 1 70.616615 0l129.142154 261.671384a39.384615 39.384615 0 0 0 29.656616 21.543385l288.768 41.944615a39.384615 39.384615 0 0 1 21.819077 67.190154l-208.97477 203.697231a39.384615 39.384615 0 0 0-11.303384 34.855385l49.309538 287.586461a39.384615 39.384615 0 0 1-57.107692 41.511385l-258.323692-135.758769a39.384615 39.384615 0 0 0-36.627693 0z" fill="#FFD42C" p-id="2141"></path></svg> &nbsp;|&nbsp; <a href="javascript:;" ><span class="review-count">${productList[i].reviewCount}</span>条评论></a></div>
                </a>
                </div>
            </li>`;
                $(".result-list").append(ele);
            }
        }
    });
}

// ==================================产品分页==========================================
$(".prePage").click(function () {
    console.log("上一页")
    var pageNum = parseInt($(".pageNum").text());
    if (pageNum>1)
        getall(pageNum-1)
    else
        getall(1);
})
$(".nextPage").click(function () {
    console.log("下一页")
    var pageNum = parseInt($(".pageNum").text());
    var pages = parseInt($(".pages").text());
    if (pageNum<pages)
        getall(pageNum+1)
    else
        getall(pages);
})




// ==================================产品排序==========================================
$(".sort-byName-asc").click(function () {
    $(".sort-byName-asc").css({background: "#aeffe2"});
    $(".sort-bySales-desc").css({background: "#f8f8f8"});
    $(".sort-byStar-desc").css({background: "#f8f8f8"});
    $(".dropdown-toggle").css({background: "#f8f8f8"});

    sortStatus=0;
    getall(1);
})
$(".sort-bySales-desc").click(function () {
    $(".sort-byName-asc").css({background: "#f8f8f8"});
    $(".sort-bySales-desc").css({background: "#aeffe2"});
    $(".sort-byStar-desc").css({background: "#f8f8f8"});
    $(".dropdown-toggle").css({background: "#f8f8f8"});

    sortStatus=1;
    getall(1);
})
$(".sort-byStar-desc").click(function () {
    $(".sort-byName-asc").css({background: "#f8f8f8"});
    $(".sort-bySales-desc").css({background: "#f8f8f8"});
    $(".sort-byStar-desc").css({background: "#aeffe2"});
    $(".dropdown-toggle").css({background: "#f8f8f8"});

    sortStatus=2;
    getall(1);
})
$(".sort-byPrice-asc").click(function () {
    $(".sort-byName-asc").css({background: "#f8f8f8"});
    $(".sort-bySales-desc").css({background: "#f8f8f8"});
    $(".sort-byStar-desc").css({background: "#f8f8f8"});
    $(".dropdown-toggle").css({background: "#aeffe2"});

    sortStatus=3;
    getall(1);
})
$(".sort-byPrice-desc").click(function () {
    $(".sort-byName-asc").css({background: "#f8f8f8"});
    $(".sort-bySales-desc").css({background: "#f8f8f8"});
    $(".sort-byStar-desc").css({background: "#f8f8f8"});
    $(".dropdown-toggle").css({background: "#aeffe2"});

    sortStatus=4;
    getall(1);
})




// ==================================点击热词搜索==========================================
$(".search-hot-text").click(function (event) {
    console.log("ele:"+$(this).text());
    $(".search-key-input").val($(this).text());
    getall(1);
})







// ==================================添加收藏与取消收藏=====================================================
// $("div").on("click","p",function()
$("div").on("click",".favorite-add",function (event) {
    var eleDiv = event.target;
    $(eleDiv).hide();
    $(eleDiv).prev().show();
    console.log("添加收藏");
})
$("div").on("click",".favorite-reduce",function (event) {
    console.log("取消收藏");
    var eleDiv = event.target;
    $(eleDiv).hide();
    $(eleDiv).next().show();
})





function getFullDate(ms){
    var d = new Date(ms);
    var year = d.getFullYear();
    var month = d.getMonth()+1;
    var day = d.getDate();
    return year+"-" + month + "-" + day;
}

//点击搜索按钮
$(".search-btn").click(function () {
    console.log("点击搜索！")
    getall(1);
});


//进入到 职位页面，需要查询所有职位（没有条件或有条件）
getall(1);

//========================清空筛选条件=====================
$(".condition-show-clear").click(function () {
    document.querySelector(".condition-selected").value="";
});

//点击名称进入首页
$(".nav_userName").click(function () {

    $.ajax({
        url: "/order/getId",
        type: "GET",
        datatype: "json",
        success: function (data) {
            console.log(data.data)
            window.sessionStorage.setItem("cusid", data.data)
            $(location).attr('href', 'order');
        }

    })

});